package show.me.the.money.util.xml;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

@SuppressWarnings("unchecked")
public class XMLHelper {
	private static Log logger = LogFactory.getLog(XMLHelper.class);
	private static String DEFAULT_ENCODING = "utf-8";
	
	public static Document createDocument(){
		return DocumentHelper.createDocument();
	}
	
	public static  Document getDocument(String fileName) {
		return getDocument(new File(fileName));
	}
	
	public static  Document getDocument(File file) {
		try {
			return getDocument(new FileInputStream(file));
		} 
		catch (FileNotFoundException e) {
			logger.error("File " + file + " not found.", e);
		}
		return null;
	}
	
	public static Document getDocument(InputStream ins) {
		SAXReader oReader = new SAXReader();
		try {
			return oReader.read(ins);
		} catch (DocumentException e) {
			logger.error("Failed to get document.", e);
		}
		return null;
	}

	public static void putDocument(OutputStream outs, Document doc) {
		OutputFormat format = OutputFormat.createPrettyPrint();
		XMLWriter writer = null;
		try {
			format.setEncoding(DEFAULT_ENCODING);
			writer = new XMLWriter(outs, format);
			writer.setEscapeText(false);
			writer.write(doc);
		} 
		catch (Exception e) {
			logger.error("Failed to write document.", e);
		}
		finally{
			if(writer != null){
				try {
					writer.close();
				} 
				catch (IOException e) {
					logger.error(e.getMessage());
				}
			}
		}
	}
	
	public static void putDocument(File file, Document doc) {
		OutputFormat format = OutputFormat.createPrettyPrint();
		XMLWriter writer = null;
		try {
			format.setEncoding("UTF-8");
			writer = new XMLWriter(new FileOutputStream(file), format);
			writer.setEscapeText(true);
			writer.write(doc);
		} 
		catch (Exception e) {
			logger.error("Failed to write document.", e);
		}
		finally{
			if(writer != null){
				try {
					writer.close();
				} 
				catch (IOException e) {
					logger.error(e.getMessage());
				}
			}
		}
	}

	public static void ConvertAttrs(Element el,HashMap<String,String> props,String prefix){
		if(el == null){
			return;
		}
		List<Attribute> attrs = el.attributes();
		for(Iterator<Attribute> it = attrs.iterator();it.hasNext();){
			Attribute attr = it.next();
			String name = attr.getName();
			if(prefix != null && prefix.length() > 0){
				char c = name.charAt(0);
				if(c > 96 && c < 123){
					c -= 32;
				}
				name = prefix + c + name.substring(1);
			}
			String value = attr.getText();
			props.put(name, value);
		}		
	}
	

}
